package com.accounting.client.remote.enumeration;

import java.util.List;
import java.util.Map;

import corelib.client.CollectionUtil;
import corelib.client.MathUtil;
import corelib.client.delegate.Func1;
import corelib.client.linq.Linq;

public class WeightUnit {
	
	public final static WeightUnit KiloGram = new WeightUnit("KG",  "公斤", 1);
    public final static WeightUnit Gram     = new WeightUnit("G",   "克", 1000);
    public final static WeightUnit CGram    = new WeightUnit("J",   "斤", 0.5);
    
    public final static List<WeightUnit> All = CollectionUtil.newArrayList(KiloGram,Gram, CGram);
    public final static Map<String, WeightUnit> Index = Linq.toMap(All, new Func1<WeightUnit,String>(){
		@Override
		public String run(WeightUnit param) {
			return param.getName();
		}
    });
    
    public static WeightUnit GetInstance(String name) {
        return Index.get(name);
    }

    public static WeightUnit GetInstance(String name, WeightUnit def) {
        return Index.get(name) == null?  def : Index.get(name);
    }
    
	private String name; 
	private String cname;
	private double weight;
	
	private WeightUnit(String name, String cname, double weight){
		this.name = name;
		this.cname = cname;
		this.weight = weight;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}
	
	public double ConvertTo(double inputValue, WeightUnit outputUnit) {
        if (outputUnit == null) {
            return MathUtil.round(inputValue, 4);
        }
        return MathUtil.round(inputValue * (outputUnit.weight / weight), 4);
    }

    public double ConvertFrom(double inputValue, WeightUnit inputUnit) {
        if (inputUnit == null) {
            return MathUtil.round(inputValue, 4);
        }
        return MathUtil.round(inputValue * (weight / inputUnit.weight), 4);
    }

   
}
